<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12">
        <list ref="list" :loading="mergeLoading" :disabled="!$permission.canRead()">
          <template #toolbar-left>
            <div class="repeat-customer-title">英文名称重复</div>
          </template>
          <template #toolbar-right>
            <el-button @click="mergeCustomers(false)" :loading="mergeLoading" type="primary">合并客户</el-button>
          </template>
        </list>
      </el-col>
      <el-col :span="12">
        <zh-list ref="zhList" :disabled="!$permission.canRead()">
          <template #toolbar-left>
            <div class="repeat-customer-title">中文名称重复</div>
          </template>
          <template #toolbar-right>
            <el-button @click="mergeCustomers(true)" :loading="mergeZhLoading" type="primary">合并客户</el-button>
          </template>
        </zh-list>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import list from "@/components/crm/customer/repeatList";
import zhList from "@/components/crm/customer/zhRepeatList";
import api from "@/api/crm/customer"
export default {
  components: {
    list,
    zhList
  },
  data() {
    return {
      mergeLoading: false,
      mergeZhLoading: false
    };
  },
  created() { },
  mounted() {
    this.reload();
  },
  methods: {
    reload() {
      this.$refs.list.reload();
      this.$refs.zhList.reload();
    },
    mergeCustomers(isLocalization = false) {
      var rows = isLocalization ? this.$refs.zhList.getCheckedRowsWithMessage() : this.$refs.list.getCheckedRowsWithMessage();
      if (!rows || rows.length == 0) {
        return;
      }
      let message = "确定要将选中的客户进行合并去重吗？";
      let postData = {
        nameUniqueValues: rows.map(item => isLocalization ? item.nameLocalizationUniqueValue : item.nameUniqueValue),
        isLocalization
      };
      this.$confirm(message, "提示", {
        type: "warning",
      })
        .then(() => {
          if (isLocalization) {
            this.mergeZhLoading = true;
          } else {
            this.mergeLoading = true;
          }
          api.mergeCustomersExportExcel(postData).then((res) => {
            if (isLocalization) {
              this.mergeZhLoading = false;
              this.$refs.zhList.reload();
            } else {
              this.mergeLoading = false;
              this.$refs.list.reload();
            }
          });
        })
        .catch((e) => {
          console.log(e);
        });
    },
  },
};
</script>
<style>
.repeat-customer-title {
  font-size: 14px;
  line-height: 2;
  color: #606266;
}
</style>